package com.morehair.service.wx.impl;

import com.github.pagehelper.PageInfo;
import com.morehair.bean.bo.admin.BaseReq;
import com.morehair.bean.bo.admin.CouponReq;
import com.morehair.bean.bo.wx.*;
import com.morehair.bean.pojo.Coupon;
import com.morehair.bean.pojo.CouponExample;
import com.morehair.bean.pojo.CouponUser;
import com.morehair.bean.pojo.CouponUserExample;
import com.morehair.bean.vo.wx.WxBaseData;
import com.morehair.bean.vo.wx.WxCheckoutVo;
import com.morehair.bean.vo.wx.WxCouponMyListVO;
import com.morehair.controller.wx.WxCartController;
import com.morehair.mapper.CouponMapper;
import com.morehair.mapper.CouponUserMapper;
import com.morehair.service.wx.WxCouponService;
import com.morehair.utils.AdPageHelper;
import com.morehair.utils.GetNowUser;
import com.morehair.utils.WxPageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: DLX
 * @Date: 2021/6/8 10:05
 * @Description:
 **/

@Service
@Transactional
public class WxCouponServiceImpl implements WxCouponService {

    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;

    @Override
    public WxBaseData<Coupon> wxQuery(WxBaseReq wxBaseReq) {
        WxPageHelper.startPage(wxBaseReq);

//        WxCouponReq wxCouponReq= (WxCouponReq) wxBaseReq;
//        Short status = wxCouponReq.getStatus();
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();

        criteria.andDeletedEqualTo(false);
//        if (status != null) {
//            criteria.andStatusEqualTo(status);
//        }

        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        long total = couponPageInfo.getTotal();

        return WxBaseData.ok(coupons, total);
    }

    @Override
    public WxBaseData<Coupon> wxQueryMyCoupon(WxBaseReq wxBaseReq) {
        WxPageHelper.startPage(wxBaseReq);
        WxCouponReq wxCouponReq = (WxCouponReq) wxBaseReq;
        // 根据userId 从couponuser 中查询
        // 根据 couponId  和 status 从coupon 中查询
        Integer userId = GetNowUser.getUser().getId();
        Short status = wxCouponReq.getStatus();
//        Integer size = wxCouponReq.getSize();

        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();

//        criteria.andDeletedEqualTo(false).andUserIdEqualTo(userId);
        criteria.andUserIdEqualTo(userId);
        List<WxCouponMyListVO> wxCouponMyListVOs = new ArrayList<>();
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        for (CouponUser couponUser : couponUsers) {
            Date startTime = couponUser.getStartTime();
            Date endTime = couponUser.getEndTime();
            Coupon coupon = couponMapper.selectMycouponList(couponUser.getCouponId(), status);
            if (coupon != null) {
                WxCouponMyListVO wxCouponMyListVO = new WxCouponMyListVO(coupon.getId(), coupon.getName(), coupon.getDesc(), coupon.getTag(), coupon.getMin(), coupon.getDiscount(), startTime, endTime);
                wxCouponMyListVOs.add(wxCouponMyListVO);
            }
        }

        PageInfo<WxCouponMyListVO> couponPageInfo = new PageInfo<>(wxCouponMyListVOs);
        long total = couponPageInfo.getTotal();

        return WxBaseData.ok(wxCouponMyListVOs, total);

    }

    @Override
    public Integer couponReceice(CouponReceiveBO couponReceiveBO) {
        Integer couponId = couponReceiveBO.getCouponId();
        Coupon coupon = couponMapper.selectByPrimaryKey(couponId);
//        int ret = 0;
        // 优惠券数量
        Integer couponTotal = coupon.getTotal();
        if (couponTotal <= 0) {
            return 1;
        }
        Integer userId = GetNowUser.getUser().getId();
        // 用户领取限制
        Short limit = coupon.getLimit();
        // 用户是否有
        Integer count = couponUserMapper.queryCouponExist(userId, couponId);
        if (limit == 1 && count != 0) {
            return 2;
        }

        Date date = new Date();// 当前时间
        Date startTime = coupon.getStartTime();
        Date endTime = coupon.getEndTime();
        if (coupon.getStartTime() != null && coupon.getEndTime() != null) {
            // 领取时间判断
            if (date.before(startTime)) {
                return 3;
            }
            if (date.after(endTime)) {
                return 3;
            }
        }

        // 领取成功 往couponuser里存
        Short days = coupon.getDays();
        Calendar rightNow = Calendar.getInstance();
        startTime = rightNow.getTime();
        rightNow.setTime(date);
        rightNow.add(Calendar.DAY_OF_YEAR,days);
        endTime=rightNow.getTime();
        CouponUser couponUser = new CouponUser(null,userId,couponId,coupon.getStatus(),null,startTime,endTime,null,date,date,null);
        couponUserMapper.insertSelective(couponUser);
        // 领取一张少一张
        couponTotal--;
        coupon.setTotal(couponTotal);
        couponMapper.updateByPrimaryKeySelective(coupon);
        return 0;
    }

    @Override
    public Integer couponExchange(WxCouponExchangeBO wxCouponExchangeBO) {
        String code = wxCouponExchangeBO.getCode();
        Coupon coupon = couponMapper.selectByCode(code);
        Integer userId = GetNowUser.getUser().getId();
//        int ret = 0;
        if (coupon == null) {
            return  4;
        }
        Integer couponId = coupon.getId();
        // 优惠券数量
        Integer couponTotal = coupon.getTotal();
        if (couponTotal <= 0) {
            return 1;
        }

        // 用户领取限制
        Short limit = coupon.getLimit();
        // 用户是否有
        Integer count = couponUserMapper.queryCouponExist(userId, couponId);
        if (limit == 1 && count != 0) {
            return  2;
        }
        // 领取时间判断
        Date date = new Date();// 当前时间
        Date startTime = coupon.getStartTime();
        Date endTime = coupon.getEndTime();
        if (coupon.getStartTime() != null && coupon.getEndTime() != null) {
            // 领取时间判断
            if (date.before(startTime)) {
                return 3;
            }
            if (date.after(endTime)) {
                return 3;
            }
        }
        // 领取成功 往couponuser里存
        Short days = coupon.getDays();
        Calendar rightNow = Calendar.getInstance();
        startTime = rightNow.getTime();
        rightNow.setTime(date);
        rightNow.add(Calendar.DAY_OF_YEAR,days);
        endTime=rightNow.getTime();
        CouponUser couponUser = new CouponUser(null,userId,couponId,coupon.getStatus(),null,startTime,endTime,null,date,date,null);
        couponUserMapper.insertSelective(couponUser);
        // 领取一张少一张
        couponTotal--;
        coupon.setTotal(couponTotal);
        couponMapper.updateByPrimaryKeySelective(coupon);

        return 0;
    }

    @Override
    public List<Coupon> couponSelect(WxCouponSelectBO wxCouponSelectBO, WxCheckoutVo wxCheckoutVo) {
        Double orderTotalPrice = wxCheckoutVo.getOrderTotalPrice();
        Integer cartId = wxCouponSelectBO.getCartId();

        //////////////////////////////
        Integer userId = GetNowUser.getUser().getId();
        Short status = 0;
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();

//        criteria.andDeletedEqualTo(false).andUserIdEqualTo(userId);
        criteria.andUserIdEqualTo(userId);
        List<Coupon> coupons = new ArrayList<>();
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        for (CouponUser couponUser : couponUsers) {
            Coupon coupon = couponMapper.selectAvailablecouponList(couponUser.getCouponId(), status, orderTotalPrice);
            if (coupon != null) {
                coupons.add(coupon);
            }
        }

        /////////////////////////

        return coupons;
    }
}
